package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.TeaBean;
import org.lanqiao.tjut.model.ModelFactory;
import org.lanqiao.tjut.myUtils.MyUtils;

/**
 * Servlet implementation class UserLoginServlet2
 * action属性：UserLoginServlet
 */
@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 输出提示信息
		System.out.println();
		//获取邮箱
		String strNum = request.getParameter("textNum");
		//输出邮箱
		System.out.println("获取的客户端发送的Num:"+strNum);
		//获取密码
		String strPSW = request.getParameter("textPsw");
		//输出密码
		System.out.println("获取的客户端发送的PSW："+strPSW);
		//使用实体类进行传参 
		TeaBean adminB = MyUtils.getNewInstance(TeaBean.class);
		//将前台的页面传入值 赋值给实体变量
		adminB.setT_number(strNum);
		adminB.setT_password(strPSW);
		//调用model层进行登录验证的业务逻辑处理
		//获取用户登录信息
	    List<TeaBean> lstTea = ModelFactory.getUserLoginModelInstance().getUserLoginInfo(adminB);
	    //跳转到登录成功的页面，页面跳转之后的代码不会再被执行
	    //判断是否登录成功
	    if(lstTea != null && lstTea.size()>0){
	    	//登录成功
	    	//进行跳转
	    	request.getSession().setAttribute("loginedAdminB",lstTea.get(0));
	    	request.getRequestDispatcher("WEB-INF/logined/logined.jsp").forward(request, response);
	    	//response.sendRedirect("logined.jsp");
	    }else{
	    	//登录失败
	    	response.sendRedirect("login.jsp");
	    	request.setAttribute("m", "账号或密码错，误请重新登录");
	    }
	
	}

}
